package com.six.backend.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.six.backend.annotation.OperLog;
import com.six.backend.service.LogService;
import com.six.backend.shiro.Utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: skyler
 * @Date: 2021/11/21 13:00
 */
@RestController
@ResponseBody
@Transactional(rollbackFor=Exception.class)
@RequestMapping(value = "/Log",produces = "application/json;charset = utf-8")
public class LogController {

    @Autowired
    private LogService logService;

    @GetMapping("/getSystemLog")
    @ApiOperation("获取系统日志信息")
    @OperLog(operModule = "系统管理模块",operType = "get",operDesc = "获取系统日志操作")
    public Result getSystemLog(@RequestParam(required = false) String documentName,
                               @RequestParam Integer pageSize,
                               @RequestParam Integer pageNum) {

        PageHelper.startPage(pageNum, pageSize);
        return new Result<>().setCode(200).setMessage("操作成功").setData(new PageInfo(logService.selectSystemLog(documentName)));
    }

    @GetMapping("/getBusinessLog")
    @ApiOperation("查看业务日志")
    @OperLog(operModule = "系统管理模块",operType = "get",operDesc = "查看业务日志操作")
    public Result getBusinessLog(@RequestParam(required = false) String documentName,
                               @RequestParam Integer pageSize,
                               @RequestParam Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        return new Result<>().setCode(200).setMessage("操作成功").setData(new PageInfo(logService.selectBusinessLog(documentName)));
    }


    @GetMapping("downloadLog")
    @ApiOperation("下载日志信息文件")
    @OperLog(operModule = "系统管理模块",operType = "get",operDesc = "下载日志文件操作")
    public void downloadLog(@RequestParam String fileName, HttpServletResponse response) throws IOException {
        logService.downloadLog(fileName,response);
    }

    @DeleteMapping("/deleteLoginLog")
    @ApiOperation("清空日志")
    public Result deleteLoginLog() {
        if (logService.cleanLoginLog()) {
            return new Result<>().setCode(200).setMessage("日志已清空");
        }
        return new Result<>().setCode(202).setMessage("日志清空失败");
    }
}
